import { apiClient } from "./client";
import type { 
  PhysicalCardApplyRequest, 
  PhysicalCardApplyResponse, 
  PhysicalCardAssignRequest, 
  PhysicalCardSettingInfo,
  PhysicalCardAssignResponse,
  PhysicalCardActivateRequest,
  CardCategoryRequest,
  CardCategoryResponse,
  CardProduct,
} from "@/types/physical-card";

export const physicalCardApi = {
  // 申请实体卡
  applyPhysicalCard: async (data: PhysicalCardApplyRequest): Promise<string> => {
    return apiClient.post("/api/v1/physical-card/apply", data);
  },

  // 获取实体卡申请列表
  getPhysicalCardApplyList: async (params: {
    page: number;
    pageSize: number;
  }): Promise<PhysicalCardApplyResponse> => {
    // get方法已经返回了response.data.data (即API响应中的data字段)
    const result = await apiClient.get<PhysicalCardApplyResponse>("/api/v1/physical-card/apply-list", { 
      params: {
        Page: params.page,
        PageSize: params.pageSize
      } 
    });
    // 将Total和List字段的值复制到小写字段中，确保兼容性
    // 这样无论调用方使用大写还是小写属性名都能正常工作
    if (result) {
      if (result.List && !result.list) {
        result.list = result.List;
      }
      if (result.Total !== undefined && result.total === undefined) {
        result.total = result.Total;
      }
    }
    return result;
  },

  // 绑定实体卡
  assignPhysicalCard: async (data: PhysicalCardAssignRequest): Promise<PhysicalCardAssignResponse> => {
    return apiClient.post("/api/v1/physical-card/assign", data);
  },

  // 获取实体卡相关配置信息
  getPhysicalCardSettingInfo: async (params): Promise<PhysicalCardSettingInfo> => {
    return apiClient.get("/api/v1/physical-card/setting-info", {params});
  },

  // 激活实体卡
  activatePhysicalCard: async (data: PhysicalCardActivateRequest): Promise<any> => {
    return apiClient.post("/api/v1/physical-card/activate", data);
  },

  // 获取卡产品列表（卡bin）
  getCardProducts: async (): Promise<CardProduct[]> => {
    return apiClient.get("/api/v1/physical-card/card-product");
  },
  getCardCategory: async (params: CardCategoryRequest): Promise<CardCategoryResponse[]> => {
    return apiClient.get("/api/v1/card-category/select-list", { params });
  },
};
